package com.example.xunua_mymusic;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class guideActivity extends AppCompatActivity {
    ViewPager viewPager;
    ImageView img1,img2,img3;
    List<View> vplist=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        viewPager=findViewById(R.id.guide_viewpager);
        img1=findViewById(R.id.guide_img_first);
        img2=findViewById(R.id.guide_img_second);
        img3=findViewById(R.id.guide_img_third);
        LayoutInflater layoutInflater=LayoutInflater.from(this);
        View vp1 = layoutInflater.inflate(R.layout.viewpager_first, null);
        View vp2 = layoutInflater.inflate(R.layout.viewpager_second, null);
        View vp3 = layoutInflater.inflate(R.layout.viewpager_third, null);
        vplist.add(vp1);
        vplist.add(vp2);
        vplist.add(vp3);
        viewPager.setAdapter(new MyAdapter());
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        img1.setImageResource(R.mipmap.page_now);
                        img2.setImageResource(R.mipmap.page);
                        break;
                    case 1:
                        img1.setImageResource(R.mipmap.page);
                        img2.setImageResource(R.mipmap.page_now);
                        img3.setImageResource(R.mipmap.page);
                        break;
                    case 2:
                        img2.setImageResource(R.mipmap.page);
                        img3.setImageResource(R.mipmap.page_now);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        Button btn=vp3.findViewById(R.id.guide_btn_start);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(guideActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private class MyAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return vplist.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view==object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(vplist.get(position));
            return vplist.get(position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//            super.destroyItem(container, position, object);
            container.removeView(vplist.get(position));
        }
    }
}
